package com.stockmonitor;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * Created by NanJiang on 2017/8/2.
 */
public class RealMonitor {

    public static void main(String[] args) throws IOException {


        String stockCode = getStockCode();

//        List<String> list = Arrays.asList("sh000001","sz399001","sz399006","sz002074","sz002466","sh600016","sz300315","sz300070","sh600958","SZ300024","SZ002527");
        List<String> list = Arrays.asList(getStockCode());

        String url = "http://219.142.78.242/list=sh000001,sz399001,sz399006,sz002074,sz002466,sh600016,sz300315,sz300070,sh600958";
        if(!"".equals(stockCode)) {
            url = "http://219.142.78.242/list="+stockCode;
        }

        CloseableHttpClient client = HttpClients.createDefault();

        String responseContent = null; // 响应内容
        CloseableHttpResponse response = null;

        String[] result = null;

        while (true) {

                String visitUrl = url;

                HttpGet httpGet = new HttpGet(visitUrl);

                response = client.execute(httpGet);

                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    responseContent = EntityUtils.toString(entity, "UTF-8");
                }


                StringReader sr = new StringReader(responseContent);
                char[] chars = new char[2014];


            BufferedReader br= new BufferedReader(new StringReader(responseContent));

                String line = null;

                try {
                    while((line = br.readLine())!=null) {


                        result = line.toString().split("=")[1].split(",");

                        Double td = (Double.parseDouble(result[3])/Double.parseDouble(result[2])-1.0)*100;
                        System.out.println("--------------------------------------------------------");
                        System.out.println(getStrLenth(result[0],5)+"\t"+getStrLenth(result[3],12)+"\t"+new DecimalFormat("0.00").format(td)+"%");

                    };

                }catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    sr.close();
                }

            try {
                Thread.sleep(5000);
                System.out.println("********************************************************");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }

    private static  String getStrLenth(String str,int i) {

        if(str.length()<i) {
            int ti = i - str.length();
            for(int k = 0; k < ti ; k++) {
                str = str+" ";
            }
        }
        return str;
    }

    private static String getStockCode() {

        File file = new File("./config.properties") ;
        Properties p = new Properties();

        if(file.exists()) {
            try {
                InputStream in = new FileInputStream(file);
                p.load(in);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return p.getProperty("stockCode");
        } else {
            try {
                p.load(RealMonitor.class.getClassLoader().getResourceAsStream("config.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }
    }
}
